package com.letao.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;

import com.letao.pojo.Category;
import com.letao.pojo.Evaluation;
import com.letao.pojo.Goods;
import com.letao.service.GoodsServiceInterface;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class GoodsAction extends ActionSupport {
	private GoodsServiceInterface goodsServiceInterface;
	private List<Category> categoryList;
	private List<Goods> goodsList;
	private List<Evaluation> evaluationList;
	private Goods goods;
	private Category category;
	private List<String> imageList;
	public String getCategoryCollection()throws Exception{
		categoryList=goodsServiceInterface.getAllCategory();
		
		return INPUT;
	}
	public String getGoodsByCategory()throws Exception{//用于类别下所有物品的查看
		goodsList=goodsServiceInterface.getGoodsByCategory(getCategory().getCid());
		ServletContext servletContext = (ServletContext)ActionContext.getContext().get(ServletActionContext.SERVLET_CONTEXT);
		String rootPath = servletContext.getRealPath("/");
		for(Goods goods:goodsList){
			File file = new File(rootPath+"upload/"+goods.getGid());
			if(file.exists()&&file.isDirectory()){
	    		File[] fs=file.listFiles();
	    		if(fs.length>0) goods.setGimage(goods.getGid()+"/"+fs[0].getName());
	    		else goods.setGimage("backup/backup.jpg");
			}else goods.setGimage("backup/backup.jpg");
		}
		return INPUT;
	}
	public String getImageListByGoodsId()throws Exception{
		imageList=new ArrayList<String>();
		ServletContext servletContext = (ServletContext)ActionContext.getContext().get(ServletActionContext.SERVLET_CONTEXT);
		String rootPath = servletContext.getRealPath("/");
		File file = new File(rootPath + "upload/" + goods.getGid());
		if (file.exists() && file.isDirectory()) {
			File[] fs = file.listFiles();
			if (fs.length == 0)
				imageList.add( "backup/backup.jpg");
			for (File tempFile : fs) {
				imageList.add(goods.getGid() + "/" + tempFile.getName());
			}
		}else imageList.add("backup/backup.jpg");
		return INPUT;
	}
	public String getGoodsByGoodsId()throws Exception{
		goods=goodsServiceInterface.getGoodsByGoodsId(goods.getGid());
		return INPUT;
	}
	public String getEvaluationByGoodsId()throws Exception{
		evaluationList=goodsServiceInterface.getEvaluationByGoodsId(goods.getGid());
		return INPUT;
	}
	public String getGoodsByHot()throws Exception{
		goodsList=goodsServiceInterface.getGoodsByHot();
		ServletContext servletContext = (ServletContext)ActionContext.getContext().get(ServletActionContext.SERVLET_CONTEXT);
		String rootPath = servletContext.getRealPath("/");
		for(Goods goods:goodsList){
			File file = new File(rootPath+"upload/"+goods.getGid());
			if(file.exists()&&file.isDirectory()){
	    		File[] fs=file.listFiles();
	    		if(fs.length>0) goods.setGimage(goods.getGid()+"/"+fs[0].getName());
	    		else goods.setGimage("backup/backup.jpg");
			}else goods.setGimage("backup/backup.jpg");
		}
		return INPUT;
	}
	/*get set methods*/
	@JSON(serialize=false)
	public GoodsServiceInterface getGoodsServiceInterface() {
		return goodsServiceInterface;
	}
	public void setGoodsServiceInterface(GoodsServiceInterface goodsServiceInterface) {
		this.goodsServiceInterface = goodsServiceInterface;
	}
	public List<Category> getCategoryList() {
		return categoryList;
	}
	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}
	public List<Goods> getGoodsList() {
		return goodsList;
	}
	public void setGoodsList(List<Goods> goodsList) {
		this.goodsList = goodsList;
	}
	public List<Evaluation> getEvaluationList() {
		return evaluationList;
	}
	public void setEvaluationList(List<Evaluation> evaluationList) {
		this.evaluationList = evaluationList;
	}
	public Goods getGoods() {
		return goods;
	}
	public void setGoods(Goods goods) {
		this.goods = goods;
	}
	public Category getCategory() {
		return category;
	}
	public void setCategory(Category category) {
		this.category = category;
	}
	public List<String> getImageList() {
		return imageList;
	}
	public void setImageList(List<String> imageList) {
		this.imageList = imageList;
	}
	
}
